/*
 * @Author: zhangfeng
 * @Date: 2024-01-12 15:48:34
 * @LastEditTime: 2024-01-23 15:56:35
 * @LastEditors: zhangfeng
 * @Description:
 * @FilePath: \nodeapi\src\controller\article.controller.js
 * © 2023 张峰，All rights reserved.
 */
const path = require("path");
const { exec } = require("child_process");
const {
  createArticleServer,
  getArticleServer,
  updateArticleServer,
  deleteArticleServer,
} = require("../server/article.servere");
const {
  articleCreateFormatterError,
  articleGetFormatterError,
  articleUpdateFormatterError,
  articleDeleteFormatterError,
} = require("../constants/err.type");

class ArticleController {
  async createArticle(ctx, next) {
    const data = ctx.request.body;
    console.log(data);
    try {
      const result = await createArticleServer(data);
      console.log(result);
      if (result) {
        ctx.body = {
          code: 0,
          message: "创建文章成功",
          result: result,
        };
      } else {
        ctx.body = articleCreateFormatterError;
      }
    } catch (error) {
      console.error(error);
      ctx.body = articleCreateFormatterError;
    }
  }
  async getArticle(ctx, next) {
    const data = ctx.params;
    try {
      const result = await getArticleServer(data);
      if (result) {
        ctx.body = {
          code: 0,
          message: "获取文章列表成功",
          result: result,
        };
      } else {
        ctx.body = articleGetFormatterError;
      }
    } catch (error) {
      console.error(error);
      ctx.body = articleGetFormatterError;
    }
  }
  async updateArticle(ctx, next) {
    const data = ctx.request.body;
    console.log(data);
    try {
      const result = await updateArticleServer(data);
      if (result) {
        ctx.body = {
          code: 0,
          message: "修改文章列表成功",
          result: result,
        };
      } else {
        ctx.body = articleUpdateFormatterError;
      }
    } catch (error) {
      console.error(error);
      ctx.body = articleUpdateFormatterError;
    }
  }
  async deleteArticle(ctx, next) {
    const data = ctx.request.body;
    try {
      const result = await deleteArticleServer(data);
      if (result) {
        ctx.body = {
          code: 0,
          message: "删除文章列表成功",
          result: result,
        };
      } else {
        ctx.body = articleDeleteFormatterError;
      }
    } catch (error) {
      console.error(error);
      ctx.body = articleDeleteFormatterError;
    }
  }
  async uploadImage(ctx, next) {
    const { file } = ctx.request.files;
    console.log(ctx.request.files);
    console.log("ctx.request.files");
    console.log(path.parse(file.filepath));
    if (!file) {
      return (ctx.body = unSupportedMimeType);
    } else {
      ctx.body = {
        code: 0,
        message: "封面图片上传成功",
        result: {
          article_url: "/article/" + path.basename(file.path),
        },
      };
    }
  }
  async uploadVideo(ctx, next) {
    const { file } = ctx.request.files;
    // console.log(file);
    // console.log("file");
    if (!file) {
      return (ctx.body = unSupportedMimeType);
    } else {
      try {
        // 生成封面图片的文件名和路径
        const coverName = `${file.newFilename}-cover.jpg`;
        const coverPath = path.join(
          __dirname,
          `../upload/article/${coverName}`
        );

        // 使用 ffmpeg 生成封面图片
        const ffmpegCommand = `ffmpeg -i ${file.path} -ss 00:00:01 -vframes 1 ${coverPath}`;

        // 执行 ffmpeg 命令
        exec(ffmpegCommand, (error, stdout, stderr) => {
          if (error) {
            console.error("Failed to generate cover image:", error);
            ctx.status = 500;
            ctx.body = "Failed to generate cover image";
            return;
          }

          // TODO: 进行视频处理操作，例如保存到数据库或其他操作
        });
        ctx.body = {
          code: 0,
          message: "视频上传成功",
          result: {
            article_url: "/article/" + path.basename(file.path),
            poster: `/article/${coverName}`,
          },
        };
      } catch (error) {
        console.error(error);
      }
    }
  }
}

module.exports = new ArticleController();
